<?php

class AccessModel extends BaseModel
{
	/**
	 * 获取用户角色信息
	 * @param  $uid 用户标识
	 * @return Array
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function setAccess($rid, $nodes, $nownodes)
	{
		//删除以前的权限记录
		$NodeModel = M('Node');
		//添加现有纪录
		foreach ($nodes as $node) {
			$data['role_id'] = $rid;
			$data['node_id'] = $node;
			//获取node等级
			$node = $NodeModel->find($node);
			if ($node) {
				$data['level'] = $node['level'];
			}
			$this->add($data);
		}
		//执行删除操作
		$map = array();
		foreach ($nownodes as $node) {
			if (!in_array($node, $nodes)) {
				$map['role_id'] = $rid;
				$map['node_id'] = $node;
				$this->query('DELETE FROM ' . C('DB_PREFIX') . 'access WHERE role_id = ' . $rid . ' AND node_id=' . $node);
			}
		}
	}

	/**
	 * 获取用户角色信息允许的导航信息
	 * @param  $roles 角色信息（1,3,4）
	 * @return Array
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getAccessUrl($roles)
	{
		$data = S('admin-left-nav'.$roles);
		if(!$data){
			$data = $this->query('SELECT
					DISTINCT m_access.node_id,
					m_access.role_id,
					m_access.`level`,
					m_node.`name`,
					m_node.url,
					m_node.title
					FROM
					m_access
					INNER JOIN m_node ON m_access.node_id = m_node.id
					WHERE
					m_node.is_nav = 1 AND
					m_node.`level` = 2 AND
					m_access.role_id IN (' . $roles . ')
					ORDER BY
					m_node.sort');
			S('admin-left-nav'.$roles, $data, 60);
		}
		return $data;
	}
}

?>